<?php

namespace app\rongcui\controller;

use app\department\model\DepartmentConfigItemModel;
use app\rongcui\model\RongcuiIndustryCateModel;
use cmf\controller\AdminBaseController;
use tree\Tree;

class IndustryCateController extends AdminBaseController
{
    /**
     * 行业分类管理
     * @adminMenu(
     *     'name'   => '行业分类管理',
     *     'parent' => 'business/LicensePlate/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '行业分类管理',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $industryCateModel = new RongcuiIndustryCateModel();
        $where = ['is_del' => 0];

        $lists = $industryCateModel->where($where)->select()->toArray();

        $tree = new Tree();
        $tree->icon = ['&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';

        foreach ($lists as $key => $value) {
            $lists[$key]['edit'] = url('edit', ['pid' => $value['id']]);
            $lists[$key]['add'] = url('add', ['pid' => $value['id']]);
            $lists[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
        }

        $tree->init($lists);
        $str = "<tr>
					<td>\$id</td>
					<td>\$spacer\$name</td>
					<td>\$description</td>
					<td>\$create_time</td>
					<td>
						<a href='\$edit'>编辑</a>
					</td>
                </tr>";
        $category = $tree->getTree(0, $str);
        $this->assign("category", $category);

        $this->assign('lists', $lists);
        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function add()
    {
        if ($this->request->isPost()) {
            $industryCateModel = new RongcuiIndustryCateModel();
            $arrData = $this->request->post();

            $result = $industryCateModel->allowField(true)->validate(true)->save($arrData);
            if ($result !== false) {
                $this->success(lang("ADD_SUCCESS"), url("index"));
            } else {
                $this->error($industryCateModel->getError());
            }
        }
        $pid = $this->request->param('pid', 0, 'intval');
        if ($pid) {
            $industryCateModel = new RongcuiIndustryCateModel();
            $parent = $industryCateModel->find($pid);
            $this->assign('parent', $parent);
        }
        $departmentConfigItemModel = new DepartmentConfigItemModel();
        $items = $departmentConfigItemModel
            ->where([
                'pid' => 3
            ])
            ->select();

        $this->assign('items',$items);
        return $this->fetch();
    }

    /**
     * 修改
     * @adminMenu(
     *     'name'   => '修改',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改',
     *     'param'  => ''
     * )
     */
    function edit()
    {
        $industryCateModel = new RongcuiIndustryCateModel();
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $result = $industryCateModel->allowField(true)->validate(true)->save($arrData, ["id" => $arrData["id"]]);
            if ($result !== false) {
                $this->success(lang("EDIT_SUCCESS"), url("index"));
            } else {
                $this->error($industryCateModel->getError());
            }
        }
        $intId = $this->request->param("pid", 0, 'intval');

        $result = $industryCateModel->where(["id" => $intId])->find();

        $departmentConfigItemModel = new DepartmentConfigItemModel();
        $items = $departmentConfigItemModel
            ->where([
                'pid' => 3
            ])
            ->select();

        $this->assign('items',$items);
        $this->assign('result', $result);
        return $this->fetch();
    }

    /**
     * 单选
     * @return mixed
     */
    public function select()
    {
        $id = $this->request->param('id', 0, 'intval');
        $industryCateModel = new RongcuiIndustryCateModel();

        $where = ['industry.is_del' => 0,'industry.id' => ['neq',19], 'industry.parent_id' => ['neq',0]];
        $lists = $industryCateModel
            ->alias('industry')
            ->join('__INDUSTRY_CATE__ parent','industry.parent_id = parent.id')
            ->field('industry.*,parent.name as pname')
            ->where($where)
            ->order('industry.parent_id,industry.list_order,industry.id')
            ->select();

        $this->assign('lists', $lists);
        $this->assign('id', $id);
        return $this->fetch();
    }

    /**
     * 复选
     * @return mixed
     */
    public function check()
    {
        $ids = $this->request->param('id', '');
        $ids = explode(',', $ids);
        $industryCateModel = new RongcuiIndustryCateModel();

        $where = ['industry.is_del' => 0,'industry.id' => ['neq',19], 'industry.parent_id' => ['neq',0]];
        $lists = $industryCateModel
            ->alias('industry')
            ->join('__INDUSTRY_CATE__ parent','industry.parent_id = parent.id')
            ->field('industry.*,parent.name as pname')
            ->where($where)->select();

        $this->assign('lists', $lists);
        $this->assign('ids', $ids);
        return $this->fetch();
    }
}